/**
 * 
 */
package com.healthshare.tool;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UTFDataFormatException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import com.healthshare.common.modal.Favorite;
import com.palmedia.framework.config.LocalData;
import com.palmedia.framework.zmfile.ZMFile;
import com.palmedia.framework.zmfile.ZMFilePath;


/**
 * @author xudeliang
 *
 */
public class GlobalData
{
	public static ArrayList<Favorite>favoriterecs= new ArrayList<Favorite>();
	private static GlobalData inst;
	private GlobalData()
	{
		initGlobalData();
	}
	
	private void initGlobalData()
	{
		readFavorite();
	}
	/**
	 * @return 单例模式，执行之前确保执行getInst(Context context)
	 */
	public static GlobalData getInst()
	{
		if(null ==inst)
		{
			inst = new GlobalData();
		}
		
		return inst;
	}
	
	public  boolean writeFavorite()
	{

		FileOutputStream fos=null;
		DataOutputStream dos= null;// new DataOutputStream(fos);
		String favoritePath="favorite";
		String favoriteName="favoriterec";
		boolean ret= true;
		try
		{
			ZMFilePath favorite = new ZMFilePath();
        	favorite.pushPathNode(favoritePath);
        	favorite.addFileName(favoriteName);
        	
        	ZMFile.delFile(favorite.toString());
        	
			fos = new FileOutputStream(favorite.toString(),true);
			dos=  new DataOutputStream(fos);
			int size=favoriterecs.size();
			for(int i=0;i<size;i++)
			{
				Favorite fv = favoriterecs.get(i);
				dos.writeUTF(fv.acticleid);
				dos.writeUTF(fv.title);
				dos.writeUTF(fv.time);
			}
		}
		catch (FileNotFoundException e)
		{
			ret=false;
			e.printStackTrace();
		}
		catch (UTFDataFormatException  e)
		{
			ret=false;
			e.printStackTrace();
		}
		catch (IOException  e)
		{
			ret=false;
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(null!=dos)
				dos.close();
				if(null!=fos)
				fos.close();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return ret;
	}
	private static boolean readFavorite()
	{
		boolean ret= true;
		String favoritePath="favorite";
		String favoriteName="favoriterec";
		ZMFilePath favorite = new ZMFilePath();
        favorite.pushPathNode(favoritePath);
        favorite.addFileName(favoriteName);
        	
		String localPath = favorite.toString();
		ret = new File(localPath).exists();//..fileExist(localPath);
		
		if(ret)
		{
			FileInputStream dis1=null;
			try
			{
				dis1 = new FileInputStream(localPath);
			}
			catch (FileNotFoundException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
				return false;
			}
			
			//ByteArrayInputStream dd = new ByteArrayInputStream(tmp);
			DataInputStream dis = new DataInputStream(dis1);
			boolean rett=true;
			while(rett)
			{
				try
				{
					Favorite ui = new Favorite();
					
					ui.acticleid = dis.readUTF();
					ui.title = dis.readUTF();
					ui.time = dis.readUTF();
					favoriterecs.add(ui);
				}
				catch (EOFException e)
				{
					System.out.println("readUserData>EOFException");
					rett=false;
				}
				catch (IOException e)
				{
					System.out.println("readUserData>IOException");
					rett=false;
					ret=false;
				}
				
				if(!rett)
				{
					try
					{
						if(null!=dis)
						dis.close();
						if(null!=dis1)
						dis1.close();
					}
					catch (IOException e)
					{
						e.printStackTrace();
					}
				}
			}
		}
		return ret;
	}
	
}
